import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: { requiresAuth: true },
    children: [
  
      {
        path: '/content',
        name: 'Content',
        component: () => import('../views/contents/content/index.vue'),
        meta: { title: '内容管理' }
      },
      {
        path: '/categories',
        name: 'Categories',
        component: () => import('../views/contents/categories/categories.vue'),
      },
      {
        path: '/tag',
        name: 'Tag',
        component: () => import('../views/contents/tag/tag.vue'),
      },
      {
        path: '/comment',
        name: 'Comment',
        component: () => import('../views/contents/comment/comment.vue'),
      },
      {
        path: '/mediaResources',
        name: 'UmediaResourcessers',
        component: () => import('../views/contents/mediaResources/mediaResources.vue'),
      },
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: () => import('../views/Dashboard.vue'),
        meta: { title: '控制台' }
      },
      {
        path: '/user',
        name: 'User',
        component: () => import('../views/users/userList/index.vue'),
      },
      {
        path: '/role',
        name: 'Role',
        component: () => import('../views/users/roleList/index.vue'),
      },
      {
        path: '/userBehaviors',
        name: 'UserBehaviors',
        component: () => import('../views/users/userBehaviors/userBehaviors.vue'),
      },
      {
        path: '/userFollows',
        name: 'UserFollows',
        component: () => import('../views/users/userFollows/userFollows.vue'),
      },
      {
        path: '/userAdd',
        name: 'UserAdd',
        component: () => import('../views/users/userAdd/index.vue'),
      },
      {
        path: '/advertisers',
        name: 'Advertisers',
        component: () => import('../views/Advertisement/advertisers/advertisers.vue'),
      },
      {
        path: '/adCampaigns',
        name: 'AdCampaigns',
        component: () => import('../views/Advertisement/adCampaigns/adCampaigns.vue'),
      },
      {
        path: '/targetAudienceForm',
        name: 'TargetAudienceForm',
        component: () => import('../views/Advertisement/adCampaigns/TargetAudienceForm.vue'),
      },
      {
        path: '/adCreatives',
        name: 'AdCreatives',
        component: () => import('../views/Advertisement/adCreatives/adCreatives.vue'),
      },
      {
        path: '/adImpressions',
        name: 'adImpressions',
        component: () => import('../views/Advertisement/adImpressions/adImpressions.vue'),
      },
      {
        path: '/adEffect',
        name: 'AdEffect',
        component: () => import('../views/Advertisement/adEffect/adEffect.vue'),
      },
      {
        path: '/baseDicts',
        name: 'BaseDicts',
        component: () => import('../views/systemSettings/baseDicts/baseDicts.vue'),
      },
      {
        path: '/sysOperationLog',
        name: 'SysOperationLog',
        component: () => import('../views/sysOperationLog/sysOperationLog.vue'),
      },
      {
        path: '/contentPerformanceDaily',
        name: 'ContentPerformanceDaily',
        component: () => import('../views/dataStatistics/contentPerformanceDaily/contentPerformanceDaily.vue'),
      },
      {
        path: '/userRetentionStats',
        name: 'UserRetentionStats',
        component: () => import('../views/dataStatistics/userRetentionStats/userRetentionStats.vue'),
      },
      {
        path: '/userBehaviorCategoryStats',
        name: 'UserBehaviorCategoryStats',
        component: () => import('../views/dataStatistics/userRetentionStats/UserBehaviorCategoryStats.vue'),
      },
      {
        path: '/adPerformanceDaily',
        name: 'AdPerformanceDaily',
        component: () => import('../views/dataStatistics/adPerformanceDaily/adPerformanceDaily.vue'),
      },
      {
        path: '/systemOperationDaily',
        name: 'SystemOperationDaily',
        component: () => import('../views/dataStatistics/systemOperationDaily/systemOperationDaily.vue'),
      },
      {
        path: '/welcome',
        name: 'Welcome',
        component: () => import('@/views/Welcome.vue'),
        meta: { title: '欢迎' }
      }, 
       {
        path: '/contentModeration',
        name: 'ContentModeration',
        component: () => import('../views/contents/contentModeration/contentModeration.vue'),
      },

    ],



  },
   // 标签管理
  {
    path: '/tag',
    component: () => import('@/views/contents/tag/tag.vue'),
    meta: { title: '标签管理' }
  },
  
  // 评论管理
  {
    path: '/comment',
    component: () => import('@/views/contents/comment/comment.vue'),
    meta: { title: '评论管理' }
  },
  
  // 媒体资源管理
  {
    path: '/mediaResources',
    component: () => import('@/views/contents/mediaResources/mediaResources.vue'),
    meta: { title: '媒体资源管理' }
  },

]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')

  if (to.meta.requiresAuth && !token) {
    // 需要登录但未登录，重定向到登录页
    next('/login')
  } 
  else if (to.path === '/login' && token) {
    // 已登录但访问登录页，重定向到首页
    next('/welcome')
  } 
  else {
    // 设置页面标题
    document.title = to.meta.title || '后台管理系统'
    next()
  }
})

export default router
